package com.gljx.web.service.impl;

import com.gljx.util.GetFid;
import com.gljx.util.GetFnumber;
import com.gljx.util.TimeFormat;
import com.gljx.web.Entity.*;
import com.gljx.web.dao.*;
import com.gljx.web.service.CustomLogService;
import com.gljx.web.service.PersonService;
import com.gljx.web.service.SharecarsubService;
import com.gljx.web.service.SharecartworegService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * Created by Administrator on 2017/12/5 0005.
 */
@Transactional
@Service
public class SharecarsubServiceImpl extends CommonDao implements SharecarsubService {

    @Autowired
    SharecartworegentryMapper sharecartworegentryMapper;
    @Autowired
    SharecarthreeregentryMapper sharecarthreeregentryMapper;
    @Autowired
    SharecarsubMapper sharecarsubMapper;
    @Autowired
    SharecarsubentryMapper sharecarsubentryMapper;
    @Autowired
    DropexercisetwoentryMapper dropexercisetwoentryMapper;
    @Autowired
    DropexercisethreeMapper dropexercisethreeMapper;
    @Autowired
    StudentMapper studentMapper;
    @Autowired
    SharecartworegService sharecartworegService;
    @Autowired
    SharecartworegMapper sharecartworegMapper;
    @Autowired
    CustomLogService customLogService;
    @Autowired
    SharecarthreeregMapper sharecarthreeregMapper;
    @Autowired
    PersonService personService;
    @Autowired
    private StudentInitiativeCarMapper studentInitiativeCarMapper;
    @Autowired
    private HttpSession session;

    @Override
    public Map<String, Object> selectPage(int pindex, String psql, int psize) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("Pindex", pindex);
        paramMap.put("Psql", psql);
        paramMap.put("Psize", psize);
        paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
        paramMap.put("v_cur", oracle.jdbc.OracleTypes.CURSOR);
        this.getSqlSession().select("com.gljx.web.dao.SharecarsubMapper.pagemap", paramMap, null);
        return paramMap;
    }

    @Override
    public Map<String, Object> selectPageCount(String psql) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("Psql", psql);
        paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
        this.getSqlSession().select("com.gljx.web.dao.SharecarsubMapper.pagecount", paramMap, null);
        return paramMap;
    }

    //在线分车 cflcsjid 练车时间id  carType 车型 number 人数 kemu 科目

    /**
     * @Author lcx
     * @Date 2018/1/5 0005 下午 5:16
     * <p>
     * 此方法 用于系统自动分车
     * 1.分车规则 首先匹配 这个学生 有没有原教练（是不是第二次分车） 如果有 分车则到原来的教练下
     * 2.如果这个学生 有预约教练 则 会分配到预约的教练下
     * 3.如果 学生 是三人一组 或两人一组 则会分配在一辆车
     * 4.优先级顺序 补考>预约>普通
     * 5.如果这辆车坐满了 则会继续分配下一辆车 并没有优先分配3的学生 有可能出现 车位位12  学生12 只分配了十个学生 的情况（以后改进）
     * 6.分配之后可在科目二科目三分车列表中修改 审核
     * 7.代码 首先查询 这个练车时间下 所有预约成功的学员  然后 去掉 在这个练车时间下已经分车成功的学生
     * 之后开始 优先级高的分车   然后 开始第二次分车 分配优先级低的学员 如果车上位置不够 则跳过这辆车
     * 8.如果抛出 一对多异常 说明这个学生在这个练车时间下重复分车 需要删除
     * 9.如果抛出空异常 一般为预约表中记录异常
     */
    @Override
    public Map<String, SharecarsubView> autoCar(String cflcsjid, String carType, String number, String kemu, String cfcoachid) {

        User user = (User) session.getAttribute("user");

        //1.按照班级查询可以分车的学生数目
        //从练车预约中取数据
        //根据练车时间来的  不区分科目
        List<Sharecarsub> sharecarregentryList = new ArrayList<>();
        //需要在分车的学员
        List<Sharecarsub> sharecarregentryListtem = new ArrayList<>();
        //需要去除的人
        List<Sharecartworeg> sharecartworegList = new ArrayList<>();
        List<Sharecarthreereg> sharecarthreeregList = new ArrayList<>();
        //返回界面对象
        Map<String, SharecarsubView> viewMap = new HashMap<>();
        SharecarsubView sharecarsubView = new SharecarsubView();
        viewMap.put("success", sharecarsubView);
        //成功学生
        List<String> list = new ArrayList<>();
        //分配教练
        List<String> list2 = new ArrayList<>();
        //失败学生
        List<String> list3 = new ArrayList<>();
        //需要分车的学生
        Set<String> set3 = new HashSet<>();
        //第一次成功的 学生
        Set<String> successOneStu = new HashSet<>();


        //分车前判断
        if ("".equals(cflcsjid)) {
            sharecarsubView.setErrMsr("错误！不能不选择练车时间！");
            viewMap.put("error", sharecarsubView);
            return viewMap;
        }
        /*if ("".equals(cfcoachid)) {
            sharecarsubView.setErrMsr("错误！不能不选择教练！");
            viewMap.put("error", sharecarsubView);
            return viewMap;
        }*/

        if ("2".equals(kemu)) {
            sharecarregentryList = sharecarsubMapper.selectpractiseOrderList(cflcsjid, "1");
            //取出已经分车的人
            //查询科目二分车中已经分车的人
            sharecartworegList = sharecartworegMapper.selTwoCarAsPeo(cflcsjid);

            if (sharecartworegList.size() != 0 && sharecarregentryList.size() != 0) {
                //去除已经分车的学生
                for (Sharecartworeg tem1 : sharecartworegList) {
                    if (tem1 == null) {
                        continue;
                    }
                    for (int i = 0; i < sharecarregentryList.size(); i++) {
                        System.out.println("-----------i:" + i);
                        System.out.println("sharecarregentryList.size():" + sharecarregentryList.size());
                        if (tem1.getFid().equals(sharecarregentryList.get(i).getCfstudentid())) {
                            System.out.println("当前删除的索引：" + i);
                            sharecarregentryList.remove(i);
                        }
                    }
                }
            }
        }
        if ("3".equals(kemu)) {
            sharecarregentryList = sharecarsubMapper.selectpractiseOrderList(cflcsjid, "1");
            //查询科目三分车中已经分车的人
            sharecarthreeregList = sharecarthreeregMapper.selThreeCarAsPeo(cflcsjid);

            if (sharecarthreeregList.size() != 0 && sharecarregentryList.size() != 0) {
                //去除已经分车的学生
                for (Sharecarthreereg tem1 : sharecarthreeregList) {
                    if (tem1 == null) {
                        continue;
                    }
                    for (int i = 0; i < sharecarregentryList.size(); i++) {
                        if (tem1.getFid().equals(sharecarregentryList.get(i).getCfstudentid())) {
                            sharecarregentryList.remove(i);
                        }
                    }
                }
            }


        }

        //todo 国力要求 查询所有的职员

        List<Person> person_new = personService.queryPersonListAsAllPerson(user.getFcompanyid());

        cfcoachid = "";

        for(Person p:person_new){
            cfcoachid += p.getFid() + ",";
        }

        //2.计算可以分配的教练
        String[] cfcoachids = cfcoachid.split(",");

        if (sharecarregentryList != null && sharecarregentryList.size() > 0) {
            //3.比较教练数x每个车多少人
            //人数
            int num = Integer.parseInt(number);
            //教练位数
            int jlnum = cfcoachids.length;
            //学生个数
            int stunum = sharecarregentryList.size();

            //优先分车  遍历 查询 如果有选中的教练 则 调整到 指定位置

            //一辆车
            LinkedHashMap<String, List<String>> stu = new LinkedHashMap();
            List<Sharecarsubentry> sharecarsubentryList = new ArrayList<>();


            /*try {*/

            for (Sharecarsub sharecarsub : sharecarregentryList) {

                //查询子类的学生
                sharecarsubentryList = sharecarsubentryMapper.selectByFparentid(sharecarsub.getFid());

                //统计添加的人数
                addListAsSum(set3, sharecarsubentryList);

                fomatDate2(sharecartworegList, sharecarthreeregList, sharecarsubentryList);

                //加上子类的个数
                stunum += sharecarsubentryList.size();
                //进行排序
                if (sharecarsubentryList.size() >= 0 && sharecarsubentryList.size() < num) {

                    //遍历教练id队列
                    for (int i = 0; i < cfcoachids.length; i++) {
                        List<String> students1 = new ArrayList<>();
                        //优先考虑 考试没有考过的学员
                        //父类原教练id不等于空
                        if (sharecarsub.getCfoldcoachid() != null && cfcoachids[i].equals(sharecarsub.getCfoldcoachid())) {
                            //如果学生列表中有 原教练
                                if (stu.get(cfcoachids[i]) != null){
                                    students1 = stu.get(cfcoachids[i]);
                                }
                                //父类的学生
                                students1.add(sharecarsub.getCfstudentid());
                                for (Sharecarsubentry stu2 : sharecarsubentryList) {
                                    //放入子类id
                                    students1.add(stu2.getCfstudentid());
                                }
                                stu.put(cfcoachids[i], students1);
                                successOneStu.addAll(students1);

                            //如果不是预约教练则添加到第二次分车的集合中

                        }
                        //如果预约教练不等于空
                        else if (sharecarsub.getCfcoachid() != null && sharecarsub.getCfoldcoachid() == null) {
                            //如果位置不够 则换一个教练分车
                            if (sharecarsubentryList.size() + 1 > num - students1.size()) {
                                continue;
                            }
                            //判断父类的学生有没有预约教练
                            if (cfcoachids[i].equals(sharecarsub.getCfcoachid())) {
                                if (stu.get(cfcoachids[i]) != null){
                                    students1 = stu.get(cfcoachids[i]);
                                }
                                //父类的学生
                                students1.add(sharecarsub.getCfstudentid());
                                for (Sharecarsubentry stu2 : sharecarsubentryList) {
                                    //放入子类id
                                    students1.add(stu2.getCfstudentid());
                                }
                                stu.put(cfcoachids[i], students1);
                                successOneStu.addAll(students1);
                            }

                        }


                    }

                } else {
                    System.err.println("错误！人数超过了！");
                    sharecarsubView.setErrMsr("错误！人数超过了！");
                    viewMap.put("error", sharecarsubView);
                }

            }

            //复制一个新集合
             sharecarregentryListtem =  new ArrayList(sharecarregentryList) ;

            //检查 第二次添加的人数  筛选出第二次应该分车的人
            Iterator<Sharecarsub> iter = sharecarregentryListtem.iterator();
            while (iter.hasNext()) {
                Sharecarsub  s= iter.next();
                for (String successId : successOneStu) {
                    if (s.getCfstudentid().equals(successId)) {
                        iter.remove();
                    }
                }
            }


            //第二次添加 没有指定教练 不是 第二次考试的学生  按照 如果有一组的 则分配到 一组 如果这辆车位置不够 则 新换一辆车
            for (Sharecarsub sharecarsub : sharecarregentryListtem) {
                //查询子类的学生
                sharecarsubentryList = sharecarsubentryMapper.selectByFparentid(sharecarsub.getFid());

                //统计添加的人数
                addListAsSum(set3, sharecarsubentryList);

                fomatDate2(sharecartworegList, sharecarthreeregList, sharecarsubentryList);

                //加上子类的个数
                for (int i = 0; i < cfcoachids.length; i++) {
                    List<String> students = new ArrayList<>();
                    //如果这辆车还有位置
                    if (stu.get(cfcoachids[i]) == null || stu.get(cfcoachids[i]).size() < num) {
                        //如果这个教练没有被分配则进行分配
                        if (stu.get(cfcoachids[i]) == null) {
                            students.add(sharecarsub.getCfstudentid());
                            stu.put(cfcoachids[i], students);
                            for (Sharecarsubentry stu2 : sharecarsubentryList) {
                                //放入子类id
                                students.add(stu2.getCfstudentid());
                                stu.put(cfcoachids[i], students);
                            }
                            break;
                        }
                        //如果位置不够 则换一个教练分车
                        if (sharecarsubentryList.size() + 1 > num - stu.get(cfcoachids[i]).size()) {
                            continue;
                        }
                        stu.get(cfcoachids[i]).add(sharecarsub.getCfstudentid());
                        for (Sharecarsubentry stu2 : sharecarsubentryList) {
                            //放入子类id
                            stu.get(cfcoachids[i]).add(stu2.getCfstudentid());
                        }
                        break;
                    } else {
                        continue;
                    }
                }
            }

            /*} catch (Exception e) {
                System.err.println("错误！练车时间下,重复分车！");
                sharecarsubView.setErrMsr("错误！练车时间下,重复分车！");
                viewMap.put("error", sharecarsubView);
                e.printStackTrace();
            }*/

            System.out.println("学生个数：" + stunum);
            System.out.println("车位个数：" + num * jlnum);

            //科目二
            if ("2".equals(kemu)) {
                //添加主表
                Sharecartworeg sharecartworeg = new Sharecartworeg();

                //编码
                /* String numbers = customLogService.selectFunmber("CT_DS_SHARECARTWOREG",user.getFcompanyid());*/
                String fnumber = GetFnumber.fnumbers("KMEFC");
                //公司id
                sharecartworeg.setCfficompanyid(user.getFcompanyid());
                //控制单元id
                sharecartworeg.setFcontrolunitid(user.getFcompanyid());
                //获取当前修改时间
                String createTime = TimeFormat.timeFormat(new Date());
                //创建时间
                sharecartworeg.setFcreatetime(createTime);
                //修改时间
                sharecartworeg.setFcreatorid(user.getFid());
                //业务日期
                sharecartworeg.setFbizdate(TimeFormat.getNowTime());
                //编码
                sharecartworeg.setFnumber(fnumber);
                //单据状态
                sharecartworeg.setCfbillstate(Long.valueOf("3"));
                //练车时间id
                sharecartworeg.setCflcsjid(cflcsjid);
                sharecartworeg.setFdescription("科目二自动分车分配");
                String uuid = GetFid.UUIDIntoFid();
                sharecartworeg.setFid(uuid);
                sharecartworegMapper.insertSelective(sharecartworeg);

                for (int i = 0; i < cfcoachids.length; i++) {
                    if (stu.get(cfcoachids[i]) != null) {
                        System.out.println("被分车的教练：" + cfcoachids[i]);
                        for (String stuid : stu.get(cfcoachids[i])) {
                            if (stuid != null) {
                                //教练id 学生id 科目
                                System.out.println("被分车的学生：" + stuid);
                                Student student = studentMapper.selectStudentById(stuid);
                                Sharecartworegentry person = sharecartworegentryMapper.selectPersonCarByFid(cfcoachids[i]).get(0);
                                //赋值
                                Sharecartworegentry sharecartworegentry = new Sharecartworegentry();
                                sharecartworegentry.setFid(GetFid.UUIDIntoFid());
                                sharecartworegentry.setFparentid(uuid);
                                sharecartworegentry.setCfjsgroupno(person.getJlznamebm());//教练组编码
                                sharecartworegentry.setCfjlgroupid(person.getCfcoachid());//教练组id  person.getCfcoachid()
                                sharecartworegentry.setCfduizhang(person.getCfduizhang());//是否组长
                                sharecartworegentry.setCfcoachid(cfcoachids[i]);//教练id
                                sharecartworegentry.setCfcoachphone(person.getCfphone());//教练手机
                                sharecartworegentry.setCfphone(person.getCfphone());//教练电话
                                sharecartworegentry.setCfcarcx(person.getCfcarcx());//车型
                                sharecartworegentry.setCfcarnumber(person.getCfcarnumber());//车牌号
                                sharecartworegentry.setCfstudentid(student.getFid());//学生id
                                sharecartworegentry.setCfstudentno(student.getFnumber());//学生编号
                                sharecartworegentry.setCfidcardno(student.getCfidcardno());//身份号码
                                sharecartworegentry.setCfstuvip(Long.valueOf(student.getCfvip()));//是否vip
                                sharecartworegentry.setCfstubillstate(student.getCfstudentstate());//学生状态
                                sharecartworegentry.setCffristexam(student.getCfckbk());//初考补考
                                sharecartworegentry.setCfkeyhgsj(student.getCfkmyherq());//科目一合格时间
                                sharecartworegentry.setCfkscs(student.getCfkmekss());//考试次数
                                sharecartworegentry.setCflastkssj(student.getCfkmsbjgsj());//最后考试时间
                                sharecartworegentry.setCfsex(student.getCfsex());//性别（1:男，2：女）
                                sharecartworegentry.setCfphone(student.getCfsjhm());//80    手机号
                                sharecartworegentry.setCfmobile(student.getCflxdh());//电话
                                sharecartworegentry.setCfsqzjcx(student.getCfslzjcxdh());//申请准驾照车型代
                                sharecartworegentry.setCfbdyd(student.getCflocal());//（1：本地2：异地3:学生）
                                sharecartworegentry.setCfpxlx(student.getCftrainTypeNAME());//培训类型
                                sharecartworegentry.setCftjr(student.getCftjrName());//推荐人
                                sharecartworegentry.setCfclazz(student.getCfclazzName());//班级
                                sharecartworegentry.setCfyishou(student.getCfamount());//应收金额
                                sharecartworegentry.setCfysje(student.getCfreceived());//已收金额
                                sharecartworegentry.setCfyouhui(student.getCffavorable());//优惠金额
                                sharecartworegentry.setCfqianfei(student.getCfarrearage());//欠费金额
                                sharecartworegentry.setCfremark("科目二自动分车");//备注

                                list2.add(String.valueOf(person.getJlname()));//教练
                                list.add(String.valueOf(student.getFnameL2()));
                                sharecarsubView.setSuccessName(list);
                                viewMap.put("success", sharecarsubView);
                                sharecartworegentryMapper.insertSelective(sharecartworegentry);
                            }
                        }
                    }
                }
            } else if ("3".equals(kemu)) {
                //添加主表
                Sharecarthreereg sharecarthreereg = new Sharecarthreereg();
                //编码
                /* String numbers = customLogService.selectFunmber("CT_DS_SHARECARTWOREG",user.getFcompanyid());*/
                String fnumber = GetFnumber.fnumbers("KMSFC");
                //公司id
                sharecarthreereg.setCfficompanyid(user.getFcompanyid());
                //控制单元id
                sharecarthreereg.setFcontrolunitid(user.getFcompanyid());
                //获取当前修改时间
                String createTime = TimeFormat.timeFormat(new Date());
                //创建时间
                sharecarthreereg.setFcreatetime(createTime);
                //修改时间
                sharecarthreereg.setFcreatorid(user.getFid());
                //业务日期
                sharecarthreereg.setFbizdate(TimeFormat.getNowTime());
                //编码
                sharecarthreereg.setFnumber(fnumber);
                //单据状态
                sharecarthreereg.setCfbillstate(Long.valueOf("3"));
                //练车时间id
                sharecarthreereg.setCflcsjid(cflcsjid);
                sharecarthreereg.setFdescription("科目三自动分车分配");
                String uuid = GetFid.UUIDIntoFid();
                sharecarthreereg.setFid(uuid);

                sharecarthreeregMapper.insertSelective(sharecarthreereg);
                for (int i = 0; i < cfcoachids.length; i++) {
                    if (stu.get(cfcoachids[i]) != null) {
                        System.err.println("被分车的教练：" + cfcoachids[i]);
                        for (String stuid : stu.get(cfcoachids[i])) {
                            if (stuid != null) {
                                //教练id 学生id 科目
                                System.out.println("被分车的学生：" + stuid);
                                Student student = studentMapper.selectStudentById(stuid);
                                Sharecartworegentry person = sharecartworegentryMapper.selectPersonCarByFid(cfcoachids[i]).get(0);

                                //赋值
                                Sharecarthreeregentry sharecarthreeregentry = new Sharecarthreeregentry();
                                sharecarthreeregentry.setFid(GetFid.UUIDIntoFid());
                                sharecarthreeregentry.setFparentid(uuid);
                                sharecarthreeregentry.setCfjsgroupno(person.getJlznamebm());//教练组编码
                                sharecarthreeregentry.setCfjlgroupid(person.getCfcoachid());//教练组id  person.getCfcoachid()
                                sharecarthreeregentry.setCfduizhang(person.getCfduizhang());//是否组长
                                sharecarthreeregentry.setCfcoachid(cfcoachids[i]);//教练id
                                sharecarthreeregentry.setCfcoachphone(person.getCfphone());//教练手机
                                sharecarthreeregentry.setCfphone(person.getCfphone());//教练电话
                                sharecarthreeregentry.setCfcarcx(person.getCfcarcx());//车型
                                sharecarthreeregentry.setCfcarnumber(person.getCfcarnumber());//车牌号
                                sharecarthreeregentry.setCfstudentid(student.getFid());//学生id
                                sharecarthreeregentry.setCfstudentno(student.getFnumber());//学生编号
                                sharecarthreeregentry.setCfidcardno(student.getCfidcardno());//身份号码
                                sharecarthreeregentry.setCfstuvip(Long.valueOf(student.getCfvip()));//是否vip
                                sharecarthreeregentry.setCfbillstustate(student.getCfstudentstate());//学生状态
                                sharecarthreeregentry.setCffristexam(student.getCfckbk());//初考补考
                                sharecarthreeregentry.setCfkmesgsj(student.getCfkmehgrq());//科目二合格时间
                                sharecarthreeregentry.setCfkmskscs(student.getCfkmekss());//考试次数
                                sharecarthreeregentry.setCfkmsbjgsj(student.getCfkmebjgsj());//科目三不及格时间
                                sharecarthreeregentry.setCfsex(student.getCfsex());//性别（1:男，2：女）
                                sharecarthreeregentry.setCfphone(student.getCfsjhm());//80  手机号
                                sharecarthreeregentry.setCfmobile(student.getCflxdh());//电话
                                sharecarthreeregentry.setCfsqzjcx(student.getCfslzjcxdh());//申请准驾照车型代
                                sharecarthreeregentry.setCfbdyd(student.getCflocal());//（1：本地2：异地3:学生）
                                sharecarthreeregentry.setCfpxlx(student.getCftrainTypeNAME());//培训类型
                                sharecarthreeregentry.setCftjr(student.getCftjrName());//推荐人
                                sharecarthreeregentry.setCfclazz(student.getCfclazzName());//班级
                                sharecarthreeregentry.setCfyishou(student.getCfamount());//应收金额
                                sharecarthreeregentry.setCfysje(student.getCfreceived());//已收金额
                                sharecarthreeregentry.setCfyouhui(student.getCffavorable());//优惠金额
                                sharecarthreeregentry.setCfqianfei(student.getCfarrearage());//欠费金额
                                sharecarthreeregentry.setCfremark("科目三自动分车");//备注

                                list2.add(String.valueOf(person.getJlname()));//教练
                                list.add(String.valueOf(student.getFnameL2()));
                                sharecarsubView.setSuccessName(list);
                                viewMap.put("success", sharecarsubView);
                                sharecarthreeregentryMapper.insertSelective(sharecarthreeregentry);
                            }
                        }
                    }

                }
            }
        } else {
            System.err.println("没有人可以分车！");
            sharecarsubView.setErrMsr("没有人可以分车！");
            viewMap.put("error", sharecarsubView);
        }


        //计算出 可以分车的 学生
        for (Sharecarsub stu : sharecarregentryList) {
            set3.add(String.valueOf(stu.getCfstudentname()));
        }


        Map temmap = new HashMap();


        //放入map  比较出 两个集合不同的部分

        for (String tem : list) {
            temmap.put(tem, tem);
        }

        for (String tem1 : set3) {
            if (temmap.get(tem1) == null) {
                list3.add(tem1);
            }
        }

        //进行分车操作的人
        viewMap.get("success").setSumName(set3);
        //进行分车操作的人
        viewMap.get("success").setErrorName(list3);


        return viewMap;
    }

    /**
     * @Author lcx
     * @Date 2018/1/3 0003 下午 2:09
     * 子表
     */
    private void fomatDate2(List<Sharecartworeg> sharecartworegList, List<Sharecarthreereg> sharecarthreeregList, List<Sharecarsubentry> sharecarregentryList) {
        if (sharecartworegList.size() != 0 && sharecarregentryList.size() != 0) {
            //去除已经分车的学生
            for (Sharecartworeg tem1 : sharecartworegList) {
                for (int i = 0; i < sharecarregentryList.size(); i++) {
                    if (tem1 == null) {
                        continue;
                    }
                    if (tem1.getFid().equals(sharecarregentryList.get(i).getCfstudentid())) {
                        sharecarregentryList.remove(i);
                    }
                }
            }
        }
        if (sharecarthreeregList.size() != 0 && sharecarregentryList.size() != 0) {
            //去除已经分车的学生
            for (Sharecarthreereg tem1 : sharecarthreeregList) {
                if (tem1 == null) {
                    continue;
                }
                for (int i = 0; i < sharecarregentryList.size(); i++) {
                    if (tem1.getFid().equals(sharecarregentryList.get(i).getCfstudentid())) {
                        sharecarregentryList.remove(i);
                    }
                }
            }
        }
    }

    private void addListAsSum(Set<String> set3, List<Sharecarsubentry> sharecarsubentries) {
        //计算出 可以分车的 学生
        for (Sharecarsubentry stu : sharecarsubentries) {
            set3.add(String.valueOf(stu.getCfstudentName()));
        }

    }

    //添加
    @Override
    public void addsharecarsub(Sharecarsub sharecarsub, String[] zxsName, String[] zxsfnumber, String
            cflcsjid, String state) {
        //删除老的信息在次添加
        if ("1".equals(state)) {
            //查询之前的信息
            Sharecarsub sharecarsubtem = sharecarsubMapper.selectByPrimaryKey(sharecarsub.getFid());
            sharecarsub.setFcreatorid(sharecarsubtem.getFcreatorid());
            sharecarsub.setFcreatetime(sharecarsubtem.getFcreatetime());
            sharecarsubMapper.deleteByPrimaryKey(sharecarsubtem.getFid());
            sharecarsubentryMapper.deleteByFparentid(sharecarsubtem.getFid());
        }

        if ("3".equals(state)) {
            sharecarsub.setCfsubresult("1");
        }
        if ("4".equals(state)) {
            sharecarsub.setCfsubresult("2");
        }
        /*获取uuid*/
        String uuid = GetFid.UUIDIntoFid();
        sharecarsub.setFid(uuid);

        sharecarsub.setCfexercisecarplani(cflcsjid);

        /*//预约成功则写入学生表
        Student student = new Student();
        student.setFid(sharecarsub.getCfstudentid());
        //判断科目
        if ("2".equals(String.valueOf(sharecarsub.getCfsubject()))) {
            student.setCfkmelcsjid(cflcsjid);
        } else if ("3".equals(String.valueOf(sharecarsub.getCfsubject()))) {
            student.setCfkmslcsjid(cflcsjid);
        }*/

        /*studentMapper.updateByPrimaryKeySelective(student);*/

        if (zxsName != null && zxsName.length != 0) {
            //添加子类

            for (int i = 0; i < zxsName.length; i++) {
                Sharecarsubentry sharecarsubentry = new Sharecarsubentry();

                sharecarsubentry.setFid(GetFid.UUIDIntoFid());
                sharecarsubentry.setFparentid(uuid);
                sharecarsubentry.setCfstudentid(zxsName[i]);
                sharecarsubentry.setCfstudentno(zxsfnumber[i]);

                /*student.setFid(zxsName[i]);
                if ("2".equals(String.valueOf(sharecarsub.getCfsubject()))) {
                    student.setCfkmelcsjid(cflcsjid);
                } else if ("3".equals(String.valueOf(sharecarsub.getCfsubject()))) {
                    student.setCfkmslcsjid(cflcsjid);
                }*/
                /*studentMapper.updateByPrimaryKeySelective(student);*/

                if (!"".equals(zxsName[i])) {
                    sharecarsubentryMapper.insertSelective(sharecarsubentry);
                }
            }
        }
        sharecarsubMapper.insertSelective(sharecarsub);
    }

    /**
     * @Author lcx
     * @Date 2017/12/7 0007 上午 9:11
     * 删除
     */
    @Override
    public void deleteBySharecarsubentryKey(String[] deleteId) {
        for (int i = 0; i < deleteId.length; i++) {
            sharecarsubMapper.deleteByPrimaryKey(deleteId[i]);
            sharecarsubentryMapper.deleteByFparentid(deleteId[i]);
        }
    }

    /**
     * @Author lcx
     * @Date 2017/12/7 0007 上午 9:10
     * 查询更新的信息
     */
    @Override
    public Sharecarsub selectSharecarsubentryByFid(String fid) {
        return sharecarsubMapper.selectByPrimaryKey(fid);
    }

    /**
     * @Author lcx
     * @Date 2017/12/7 0007 上午 11:33
     * 查询子类信息
     */
    @Override
    public List<Sharecarsubentry> selectSharecarsubentryByFparentid(String fid) {
        return sharecarsubentryMapper.selectByFparentid(fid);
    }

    /**
     * @Author lcx
     * @Date 2017/12/8 0008 上午 10:09
     * //按照练车时间查询预约列表 练车时间 cflcsjid 科目 cfbilltype 车型 cfsqzjcxdh
     */
    @Override
    public List<Sharecarsub> selectpractiseOrderList(String cflcsjid, String cfbilltype, String
            cfsqzjcxdh, String type) {

        List<Sharecarsub> tem = new ArrayList<>();
        List<Sharecarsub> sharecarsubList = new ArrayList<>();
        //暂时使用练车时间判断 科目以及 车型不判断 type 为预约类型1为成功2为不成功

        //查询练车时间所对应的预约人列表
        tem = sharecarsubMapper.selectpractiseOrderList(cflcsjid, type);
        for (Sharecarsub stu : tem) {
            //查询这个学员id所对应的退练次数
            if ("2".equals(cfbilltype)) {
                stu.setTlcs(dropexercisetwoentryMapper.selecttlNum(stu.getCfstudentid()));
            } else {
                stu.setTlcs(dropexercisethreeMapper.selecttlNum(stu.getCfstudentid()));
            }
            sharecarsubList.add(stu);
        }

        return sharecarsubList;
    }

    @Override
    public int deleteBySharecarsubentryByFid(String fid) {
        // TODO Auto-generated method stub
        return sharecarsubMapper.deleteByPrimaryKey(fid);
    }

    /**
     * @Author lcx
     * @Date 2018/1/17 0017 下午 3:16
     * 微信预约练车
     */
    @Override
    public StudentInitiativeCar addsharecarsubAsWx(String kemu, String coachid, String lcsjId, Student student) {

        Sharecarsub sharecarsub = new Sharecarsub();
        StudentInitiativeCar studentInitiativeCar = new StudentInitiativeCar();

        String uuid = GetFid.UUIDIntoFid();

        /*String number = customLogService.selectFunmber("CT_DS_SHARECARSUB", student.getFcontrolunitid());*/
        String fnumber = GetFnumber.fnumbers("WXYYFC");
        String lcsjfid = (String) session.getAttribute("lcsjfid");

        //查询学生信息
        Student studentem = studentMapper.selectStudentById(student.getFid());

        //查询教练信息
        Person person = personService.selTrainTypeById(coachid);


        sharecarsub.setCfexercisecarplani(lcsjId);
        sharecarsub.setCfsubject(Long.valueOf(kemu));
        sharecarsub.setCfsqzjcxdh(student.getCfslzjcxdh());
        sharecarsub.setFnumber(fnumber);
        sharecarsub.setCfstudentid(student.getFid());
        sharecarsub.setCfstudentname(student.getFnameL2());
        sharecarsub.setCfsubresult("1");

        sharecarsub.setFcontrolunitid(student.getFcontrolunitid());
        sharecarsub.setFcreatetime(TimeFormat.getNowTime());
        sharecarsub.setCfcoachid(coachid);
        sharecarsub.setCfcoachname(person.getFnameL2());
        sharecarsub.setCfarrearage(studentem.getCfarrearage());
        sharecarsub.setFid(uuid);
        sharecarsub.setCfclazzid(studentem.getCfclazzid());//班级
        sharecarsub.setCfamount(studentem.getCfamount());
        sharecarsub.setCfreceived(studentem.getCfreceived());
        sharecarsub.setCffavorable(studentem.getCffavorable());
        sharecarsub.setCfkmekssj(studentem.getCfkmehgrq());
        sharecarsub.setCfkmsbjksj(studentem.getCfkmebjgsj());
        sharecarsub.setCfidcardno(studentem.getCfidcardno());
        //sharecarsub.setCfjbkcs();
        //sharecarsub.setCfkscs();
        sharecarsub.setCfstudentname(studentem.getFnameL2());
        sharecarsub.setCfcomputationtimestate("2");
        sharecarsub.setCfexercisecarplanentryid(lcsjfid);

        //如果是计时预约学员则写入标记 CFCOMPUTATIONTIMESTATE 为 1
        if ("1".equals(String.valueOf(studentem.getTimingCharge()))) {
            sharecarsub.setCfcomputationtimestate("1");
            sharecarsub.setCfbegintime(String.valueOf(session.getAttribute("stratTime")));
            sharecarsub.setCfendtime(String.valueOf(session.getAttribute("endTime")));
        }

        //如果 >= 0 则是小车
        if (student.getCfslzjcxdh().compareTo("C1") >= 0) {
            //小车
            sharecarsub.setCfcarbyte("0");
        } else {
            //大车
            sharecarsub.setCfcarbyte("1");
        }

        sharecarsubMapper.insertSelective(sharecarsub);


        //放入返回对象
        Student studenttem = (Student) session.getAttribute("weixinStudent");

        studentInitiativeCar.setLcsjfid(studenttem.getFid());
        studentInitiativeCar.setCoachName(String.valueOf(person.getFnameL2()));
        studentInitiativeCar.setStratTime(String.valueOf(session.getAttribute("stratTime")));
        studentInitiativeCar.setEndTime(String.valueOf(session.getAttribute("endTime")));
        return studentInitiativeCar;
    }

    @Override
    public List<Sharecarsub> selectSharecarsubentryXls(String cflcsj, String cfstudentname, String cfidcardno,
                                                       String cfmobile, String subject, String beginTime,
                                                       String endTime, String fcompanyid) {
        return sharecarsubMapper.selectSharecarsubentryXls(cflcsj, cfstudentname, cfidcardno, cfmobile, subject, beginTime, endTime, fcompanyid);
    }

    @Override
    public List<Sharecarsub> selectSharecarsubentryAsWxXls(String cflcsj, String cfstudentname, String cfidcardno, String cfmobile, String cfcomputationtimestate, String subject, String beginTime, String endTime, String fcompanyid) {
        return sharecarsubMapper.selectSharecarsubentryXlsAsWx(cflcsj, cfstudentname, cfidcardno, cfmobile, cfcomputationtimestate, subject, beginTime, endTime, fcompanyid);
    }

    @Override
    public List<Sharecarsub> selectSharecarsubentryXlss(String cflcsj, String cfstudentname, String cfidcardno, String cfmobile, String subject, String beginTime, String endTime,String type, String fcompanyid) {

        return sharecarsubMapper.selectSharecarsubentryXlss(cflcsj, cfstudentname, cfidcardno, cfmobile,subject, beginTime, endTime,type,fcompanyid);
    }

    /**
     * @Author lcx
     * @Date 2018/3/19 0019 下午 3:31
     * 根据 fid 和练车时间
     */
    @Override
    public void deletesharecarsubBydelfidAndLcsjid(String[] delfid) {
        for (String fid : delfid) {
            sharecarsubMapper.deleteByPrimaryKey(fid);

        }
    }

}
